package edu.visualizetion.info.sensorcollection;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hyperic.sigar.SigarException;

public class Client {

	public static void main(String[] args) {

		int size = args.length;
		long time = 0;
		// 添加网络速率
		// netSendSpeed
		// netRecvSpeed
		String header = "time,ip,cpuratio,memTotal,memUsed,memUsedRatio,memFree,memFreeRatio,netSend,netRecv,netSendSpeed,netRecvSpeed";
		String fileprex = "src/main/resources/";
		// 毫秒
		float step = 1000;

		if (size <= 0) {
			System.out.println("Please input the server ip");
			return;
		}
		Registry registry = null;
		List<IService> iServices = new ArrayList<>();
		Map<String, BufferedWriter> fileWriterMap = new HashMap<>();
		Map<String, Long> sendBytes = new HashMap<>();
		Map<String, Long> recvBytes = new HashMap<>();
		try {
			for (String host : args) {
				registry = LocateRegistry.getRegistry(host);
				IService iService = (IService) registry.lookup("iService");
				iServices.add(iService);
				String filePath = fileprex + host;
				FileWriter fileWriter = new FileWriter(filePath);
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				bufferedWriter.write(header);
				bufferedWriter.newLine();
				fileWriterMap.put(host, bufferedWriter);
			}
			while (true) {
				long count = 0;
				time++;
				count++;
				for (IService iService : iServices) {
					if (count == 200) {
						for (Entry<String, BufferedWriter> entry : fileWriterMap.entrySet()) {
							entry.getValue().close();
						}
						return;
					}
					Record record = iService.getStatus();
					record.setTime(time);
					// calculate the speed
					// byte/s
					long recv = record.getNetRecv();
					long send = record.getNetSend();
					String nameOfServer = record.getNameOfServer();
					if (recvBytes.get(nameOfServer) == null) {
						record.setNetRecvSpeed(0);
						recvBytes.put(nameOfServer, recv);
					} else {
						float recvSpeed = (recv - recvBytes.get(nameOfServer)) / (float) (step / 1000.0) / 1024 / 1024;
						record.setNetRecvSpeed(recvSpeed);
						recvBytes.put(nameOfServer, recv);
					}
					if (sendBytes.get(nameOfServer) == null) {
						record.setNetSendSpeed(0);
						sendBytes.put(nameOfServer, send);
					} else {
						float sendSpeed = (send - sendBytes.get(nameOfServer)) / (float) (step / 1000.0) / 1024 / 1024;
						record.setNetSendSpeed(sendSpeed);
						sendBytes.put(nameOfServer, send);
					}
					System.out.println(record.getNetRecvSpeed() + " " + record.getNetSendSpeed());
					fileWriterMap.get(nameOfServer).write(record.getCSV());
					fileWriterMap.get(nameOfServer).newLine();
					System.out.println(record);
				}
				Thread.sleep((long) step);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (SigarException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
